/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.module.article.controller.admin;

import com.jfinal.aop.Inject;
import io.jboot.utils.DateUtil;
import io.jboot.utils.ModelUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.model.SiteInfo;
import io.jpress.module.article.ArticlePermissionGroups;
import io.jpress.module.article.service.ArticleService;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.service.SiteInfoService;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 */
@RequestMapping(value = "/admin/article/counts", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _ArticleCountsController extends AdminControllerBase {

    @Inject
    private ArticleService articleService;


    @Inject
    private SiteInfoService siteInfoService;


    @PermissionDef(title = "查看文章数量排行", groupId = ArticlePermissionGroups.ARTICLE)
    @AdminMenu(text = "文章数量排行", groupId = JPressConsts.SYSTEM_MENU_STATISTICS, order = 0)
    public void index() {


        List<SiteInfo> allSites = ModelUtil.copy(siteInfoService.findAll());

        SiteInfo mainSite = new SiteInfo();
        mainSite.setName("主站点");
        mainSite.setSiteId(0L);
        allSites.add(mainSite);


        for (SiteInfo siteInfo : allSites) {
            Long weekCount = articleService.findCountBySiteIdAndStartTime(siteInfo.getSiteId(), DateUtil.getStartOfThisWeek());
            Long monthCount = articleService.findCountBySiteIdAndStartTime(siteInfo.getSiteId(), DateUtil.getStartOfThisMonth());
            Long quarterCount = articleService.findCountBySiteIdAndStartTime(siteInfo.getSiteId(), DateUtil.getStartOfThisQuarter());
            Long yearCount = articleService.findCountBySiteIdAndStartTime(siteInfo.getSiteId(), DateUtil.getStartOfThisYear());

            siteInfo.put("weekCount",weekCount);
            siteInfo.put("monthCount",monthCount);
            siteInfo.put("quarterCount",quarterCount);
            siteInfo.put("yearCount",yearCount);
        }

        allSites.sort((o1, o2) -> (int) (o2.getLong("weekCount") - o1.getLong("weekCount")));
        setAttr("weekSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("monthCount") - o1.getLong("monthCount")));
        setAttr("monthSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("quarterCount") - o1.getLong("quarterCount")));
        setAttr("quarterSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("yearCount") - o1.getLong("yearCount")));
        setAttr("yearSites",ModelUtil.copy(allSites));

        render("article/article_statistics_counts.html");
    }


}
